package com.bmsbosmpa.service.implement;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmsbosmpa.controller.util.Message;
import com.bmsbosmpa.dao.IUserDao;
import com.bmsbosmpa.entity.User;
import com.bmsbosmpa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserService extends ServiceImpl<IUserDao, User> implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public Message login(User user) {
        Message message = new Message();
        User selectedUser = userDao.selectById(user.getId());
        if (selectedUser == null) {
            message.setMessage("用户不存在");
            message.setStatus(1);
        } else if (!Objects.equals(selectedUser.getPassword(), user.getPassword())) {
            message.setMessage("用户名或密码错误");
            message.setStatus(1);
        } else {
            message.setMessage("登陆成功");
            message.setStatus(0);
            message.setData(JSON.toJSON(selectedUser));
        }
        return message;
    }

}
